const ResolveListModel = require('../db/models/resolve_list');


class ResolveListService { 
  async addResolveList (data) {
    const userId = data.userId;

        //检查cid是否存在 check cid is existed
    if(!userId){
      return;
    }

    const result = await ResolveListModel.findOne({ where: { userId } });

    //判断 check
    if (result) { 
      //更新 update  
      return await ResolveListModel.update(data, {where: { userId }})
    }else {
      //创建 create
      return await ResolveListModel.create(data);
    }
  }

  async getAllResolveList () {
    return await ResolveListModel.findAll();
  }

  async findResolveListById(userId) {
    const resolveList = await ResolveListModel.findOne({
      where: { userId }
    });

    return resolveList;
  }

  async deleteResolveListById (userId) {
    const result = await ResolveListModel.findOne({ where: { userId } });
    return result.destroy();
  }
}

module.exports = new ResolveListService();